package com.otomotion.research.ants.client;

import java.awt.Color;
import java.nio.FloatBuffer;

import com.jme.app.SimpleGame;
import com.jme.bounding.BoundingSphere;
import com.jme.image.Texture;
import com.jme.input.InputHandler;
import com.jme.input.KeyBindingManager;
import com.jme.input.KeyInput;
import com.jme.input.NodeHandler;
import com.jme.light.DirectionalLight;
import com.jme.light.LightNode;
import com.jme.light.PointLight;
import com.jme.math.Vector3f;
import com.jme.renderer.ColorRGBA;
import com.jme.renderer.Renderer;
import com.jme.scene.Line;
import com.jme.scene.Node;
import com.jme.scene.Skybox;
import com.jme.scene.Text;
import com.jme.scene.shape.Box;
import com.jme.scene.state.AlphaState;
import com.jme.scene.state.CullState;
import com.jme.scene.state.LightState;
import com.jme.system.DisplaySystem;
import com.jme.util.TextureManager;


public class AntViewer extends SimpleGame {

	private Node dirt;
	Skybox skybox;
	Client myClient = null;
	public static void main(String[] args) {
		AntViewer app = new AntViewer();
		app.setDialogBehaviour(ALWAYS_SHOW_PROPS_DIALOG);
		app.start();
	}

	protected void simpleUpdate() {
		
		skybox.setLocalTranslation(cam.getLocation());
        if (KeyBindingManager.getKeyBindingManager().isValidCommand("next_ant",
                false)) {
            myClient.nextAnt();
        }
        if (KeyBindingManager.getKeyBindingManager().isValidCommand("prev_ant",
                false)) {
            myClient.prevAnt();
        }
        if (KeyBindingManager.getKeyBindingManager().isValidCommand("bigger",
                false)) {
            myClient.increaseSize();
        }
        if (KeyBindingManager.getKeyBindingManager().isValidCommand("smaller",
                false)) {
            myClient.decreaseSize();
        }

		
	}

	protected void simpleInitGame() {
		rootNode.setRenderQueueMode(Renderer.QUEUE_OPAQUE);
		fpsNode.setRenderQueueMode(Renderer.QUEUE_OPAQUE);

		// Vector3f loc = new Vector3f(0.0f, 0.0f, -100.0f);
		// Vector3f left = new Vector3f(1.0f, 0.0f, 0.0f);
		// Vector3f up = new Vector3f(0.0f, 1.0f, 0.0f);
		// Vector3f dir = new Vector3f(0.0f, 0f, 1.0f);
		// cam.setFrame(loc, left, up, dir);
		// cam.update();

		display.setTitle("AntViewer");

		lightState.setEnabled(false);


		DirectionalLight dr = new DirectionalLight();
		dr.setDiffuse(new ColorRGBA(1.0f, 1.0f, 1.0f, 1.0f));
		dr.setAmbient(new ColorRGBA(0.5f, 0.5f, 0.5f, 1.0f));
		dr.setDirection(new Vector3f(0, 0, 150));
		dr.setEnabled(true);
		lightState.detachAll();
		lightState.attach(dr);
		
		dirt = new Node("dirt");
		this.buildSkyBox();
		CullState cs = display.getRenderer().createCullState();
		cs.setCullMode(CullState.CS_BACK);
		cs.setEnabled(true);
		rootNode.setRenderState(cs);
//		rootNode.setLocalScale(5f);
		rootNode.attachChild(dirt);

		String hostname = "steam";
        int port = 5635;
        myClient = new Client(hostname,port,dirt,this.display);
        myClient.start();
        KeyBindingManager.getKeyBindingManager().set("previous_ant",
                KeyInput.KEY_J);
        KeyBindingManager.getKeyBindingManager().set("next_ant",
                KeyInput.KEY_K);
        KeyBindingManager.getKeyBindingManager().set("bigger",
                KeyInput.KEY_EQUALS);
        KeyBindingManager.getKeyBindingManager().set("smaller",
                KeyInput.KEY_MINUS);
        
       
        
	}

	private Box getBox(float x,float y, float z) {
        Vector3f min = new Vector3f(-1, -1, -1);
        Vector3f max = new Vector3f(1, 1, 1);
        
		AlphaState as1 = display.getRenderer().createAlphaState();
		as1.setBlendEnabled(true);
		as1.setSrcFunction(AlphaState.SB_SRC_ALPHA);
		as1.setDstFunction(AlphaState.DB_ONE);
		as1.setTestEnabled(true);
		as1.setTestFunction(AlphaState.TF_GREATER);
		as1.setEnabled(true);
		
		Box box;
		box = new Box("box", min, max);
		box.setDefaultColor(new ColorRGBA(ColorRGBA.brown));
		box.setRenderState(as1);
		box.setModelBound(new BoundingSphere());
		box.updateModelBound();
		box.setLightCombineMode(LightState.OFF);
		box.setRenderQueueMode(Renderer.QUEUE_TRANSPARENT);
		box.setLocalTranslation(x,y,z);
		
		return box;
	}
	
	private void buildSkyBox() {
		
        skybox = new Skybox("skybox", 10, 10, 10);

        Texture north = TextureManager.loadTexture(
            AntViewer.class.getClassLoader().getResource(
            "com/otomotion/research/ants/client/white.jpg"),
            Texture.MM_LINEAR,
            Texture.FM_LINEAR);
        

        skybox.setTexture(Skybox.NORTH, north);
        skybox.setTexture(Skybox.WEST, north);
        skybox.setTexture(Skybox.SOUTH, north);
        skybox.setTexture(Skybox.EAST, north);
        skybox.setTexture(Skybox.UP, north);
        skybox.setTexture(Skybox.DOWN, north);
        skybox.preloadTextures();
//        rootNode.attachChild(skybox);
   }

}
